//
//  LSItemQuesView.m
//  听说弱点强化训练
//
//  Created by 盛超 on 2018/8/24.
//  Copyright © 2018年 盛超. All rights reserved.
//

#import "LSListenQuesView.h"
#import "LSListenSelectCell.h"
#import "BKAnswerScorllView.h"
#import "BKAudioPlayer.h"
#import "WJTimeCircle.h"
#import "KZShadowButton.h"
#import "BKListenAnswerView.h"
#import "LSTYAttributedLabel.h"
#import "LSGetZSDManager.h"
#import "LSSaveZSDManager.h"
#import "LSModelZSD.h"
#import "DicMainView.h"
#import "BKLineChartView.h"

//static NSInteger tipHeight = 150;
//static NSInteger bottomY = 200;
//static NSInteger topY = -100;

@interface LSListenQuesView ()<UITableViewDelegate,UITableViewDataSource,BKAudioPlayerDelegate,LSTYAttributedLabelDelegate,DicMainViewDelegate>

@property (nonatomic,strong) UIView *titleBarView;
/*播放器时间*/
@property (nonatomic,strong) UILabel  *timeLabel;
/*播放器滑钮*/
@property (nonatomic,strong) UISlider *slider;
/*播放按钮*/
@property (nonatomic,strong) LSCircleBtn *imagePlayerView;
/*第一次播放*/
@property (nonatomic,assign) BOOL isFirstPlay;

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) BKAnswerScorllView *scrollView;

@property (nonatomic,strong) NSMutableArray *cellRowHeightArr;

@property (nonatomic,assign) CGFloat answerViewHeight;
/*滑块*/
@property (nonatomic,strong) UIButton *sliderBtn;
/*线条*/
@property (nonatomic,strong) UILabel *lineLabel;

@property (nonatomic,strong) BKAudioPlayer *player;
/*倒计时提示*/
@property (nonatomic,strong) UIView *tipsView;

@property (nonatomic,strong) UILabel *tipsLabel;

@property (nonatomic,strong) WJTimeCircle *timeCircleView;

@property (nonatomic,strong) NSTimer *circleTimer;

@property (nonatomic,assign) CGFloat second;//倒计时的时间
/*进行的阶段*/
@property (nonatomic,assign) NSInteger step;//0-预读，1-听力，2-作答
/*是否重新作答过*/
//@property (nonatomic,assign) BOOL isReAnswer;
/*下一题*/
@property (nonatomic,strong) KZShadowButton *nextBtn;
/*重新作答*/
@property (nonatomic,strong) UIButton *reAnswerBtn;
/*回答正确*/
@property (nonatomic,strong) UIImageView *answerWrongView;
/*回答错误*/
@property (nonatomic,strong) UIImageView *answerTrueView;
/*播放速度*/
@property (nonatomic,assign) CGFloat playRate;

@property (nonatomic,strong) DicMainView *dictView;

@property (nonatomic,strong) NSMutableArray *mutlCodeArr;
@property (nonatomic,strong) NSMutableArray *mutlNameArr;
/*最后一题按钮显示*/
@property (nonatomic,assign) BOOL isNoMore;

@property (nonatomic,assign) NSInteger tipHeight;
@property (nonatomic,assign) NSInteger bottomY;
//@property (nonatomic,assign) NSInteger topY;

/*是否正在播放*/
@property (nonatomic,assign) BOOL isPlaying;

@end

@implementation LSListenQuesView

- (instancetype)initWithFrame:(CGRect)frame {
    if (self = [super initWithFrame:frame]) {
        //self.backgroundColor = [UIColor redColor];
        self.player = [[BKAudioPlayer alloc] init];
        self.player.delegateM = self;
        self.answerDict = [NSMutableDictionary dictionary];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TimeReleaseAction) name:@"TimeRelease" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TimePauseAction) name:@"TimePause" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TimeResumeAction) name:@"TimeResume" object:nil];
    
    }
    return self;
}

- (instancetype)init {
    if (self = [super init]) {
        //self.backgroundColor = [UIColor redColor];
        self.player = [[BKAudioPlayer alloc] init];
        self.player.delegateM = self;
        self.answerDict = [NSMutableDictionary dictionary];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TimeReleaseAction) name:@"TimeRelease" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TimePauseAction) name:@"TimePause" object:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(TimeResumeAction) name:@"TimeResume" object:nil];
    
    }
    return self;
}

- (void)setModel:(LSModelQues *)model {
    
    _model = model;
    
    _tipHeight = 150;
    _bottomY = 200;
//    _topY = -100;
    
    if (self.speedLayer==1) {
        _playRate = 0.8;
    } else if (self.speedLayer==2) {
        _playRate = 1.0;
    } else if (self.speedLayer==3) {
        _playRate = 1.2;
    }
    
    _isFirstPlay = YES;
    
    //初始化预读的时间
    self.second = self.model.ReadTime;
    
    [self createUI];
}

//创建视图
- (void)createUI {
   
    //标题栏
    [self addSubview:self.titleBarView];
    [self.titleBarView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.right.equalTo(self);
        make.height.mas_equalTo(35);
    }];
    
    //播放器区域（单层结构时整个试题）
    [self addSubview:self.tableView];
    self.tableView.tableHeaderView = [self createTableViewHeader];
    //倒计时提示
    if (self.answerState==2) {

        _tipHeight = 0;
        CGFloat tableViewHeight = kHeight-kNavgationBar-35-kBottomPadding;
        self.tableView.height = tableViewHeight;
        
    } else {
        [self addSubview:self.tipsView];
    }
    
    //双层听力选择添加作答区域
    if (self.model.ChildList.count>1) {
        //调整tableView高度
        CGFloat  tableViewHeight = 156*ScalesW;//kHeight/3-kNavgationBar;
        self.tableView.height = tableViewHeight;
        
        //添加作答区域
        [self addSubview:self.scrollView];
        if (self.answerState==2) {
            self.scrollView.answerState = self.answerState;
            self.scrollView.answerDict = self.answerDict;
        } else if(self.answerState==0){
            if (_answerDict.count>0) {
                self.scrollView.answerDict = self.answerDict;
            }
        }
        self.scrollView.quesModel = self.model;
        CGFloat scrollViewHeight = (kHeight-kNavgationBar-_tipHeight-kBottomPadding-35)-tableViewHeight;
        self.scrollView.height = scrollViewHeight;
        _answerViewHeight = scrollViewHeight;
        
        //滑钮
        UIButton *sliderBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        sliderBtn.frame = CGRectMake((kWidth-53*ScalesW)/2,self.scrollView.y-18*ScalesW, 53*ScalesW, 18*ScalesW);
        self.sliderBtn = sliderBtn;
        [sliderBtn setBackgroundImage:[UIImage imageNamed:@"LS-中间滑动按钮"] forState:UIControlStateNormal];
        [self addSubview:sliderBtn];
        
        //拖动手势
        UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panDragMoving:)];
        [sliderBtn addGestureRecognizer:panGesture];
        
        //横线
        UILabel *lineLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, sliderBtn.y+sliderBtn.height-1,kWidth, 0.5)];
        self.lineLabel = lineLabel;
        lineLabel.backgroundColor = [UIColor lightGrayColor];
        lineLabel.alpha = 0.8;
        [self addSubview:lineLabel];
        
    } else {
        if (self.answerState==2) {
            _tableView.tableFooterView = [self createFooterView];
        }
    }
    
    if (self.answerState!=2) {
        //开始倒计时
        [self startTimeCircle];
    }
}

#pragma mark - 滑块拖动方法
- (void)panDragMoving:(UIPanGestureRecognizer*)sender {
    
    if (sender.state==UIGestureRecognizerStateBegan)
    {
        
    }
    else if (sender.state==UIGestureRecognizerStateChanged)
    {
        CGFloat height = kHeight-kNavgationBar-_tipHeight-kBottomPadding;

        CGPoint changePoint = [sender translationInView:self];
        CGFloat panYDistance = height-_answerViewHeight-18*ScalesW+changePoint.y;
        if (panYDistance>=35 && panYDistance<=height-18*ScalesW) {
            
            self.sliderBtn.y = panYDistance;
            self.lineLabel.y = self.sliderBtn.y+self.sliderBtn.height-1;
            self.tableView.height = self.sliderBtn.y+self.sliderBtn.height;
            self.scrollView.y = self.sliderBtn.y+self.sliderBtn.height;
            
            self.scrollView.height = height - self.tableView.height;
            //self.scrollView.answerViewHeight = height - self.tableView.height;
            [self.scrollView updateCurrentViewHeight];
        }
        
    }
    else if (sender.state == UIGestureRecognizerStateEnded)
    {
       // self.scrollView.answerViewHeight = self.scrollView.height;
        [self.scrollView updateAllViewHeight];
        _answerViewHeight = self.scrollView.height;
    }
}

#pragma mark - 懒加载

- (KZShadowButton *)nextBtn {
    if (!_nextBtn) {
        WeakObj(self);
        _nextBtn = [[KZShadowButton alloc] init:^(UIButton *button) {
            button.backgroundColor = RGBA(11, 178, 242, 1);
            [button setTitle:@"下一题" forState:UIControlStateNormal];
            button.titleLabel.font = FONT(14);
            if (selfWeak.currentIndex+1 == selfWeak.allCount) {
                [button setTitle:@"查看结果" forState:UIControlStateNormal];
            }

        }];
        _nextBtn.frame = CGRectMake((kWidth-144)/2,_tipHeight+_bottomY, 143,44);//CGRectMake((kWidth-140*ScalesW)/2,tipHeight+bottomY, 140*ScalesW, 30*ScalesW);
        [_nextBtn addTarget:self action:@selector(nextBtnAction:) forControlEvents:UIControlEventTouchUpInside];
        if(_isNoMore) {
            [_nextBtn.button setTitle:@"没有更多" forState:UIControlStateNormal] ;
            _nextBtn.button.backgroundColor = [UIColor colorWithHex:0xe1e2e2];
            _nextBtn.enabled = NO;
        }
    }
    return _nextBtn;
}

- (UIButton *)reAnswerBtn {
    if (!_reAnswerBtn) {
//        _reAnswerBtn = [[KZShadowButton alloc] init:^(UIButton *button) {
//            button.backgroundColor = RGBA(11, 178, 242, 1);
//            [button setTitle:@"重新作答" forState:UIControlStateNormal];
//            button.titleLabel.font = FONT(14);
//        }];
        _reAnswerBtn = [[UIButton alloc] init];
        _reAnswerBtn.titleLabel.font = FONT(14);
        _reAnswerBtn.layer.cornerRadius = 6;
        _reAnswerBtn.layer.masksToBounds = YES;
        _reAnswerBtn.layer.borderWidth = 1.0f;
        _reAnswerBtn.layer.borderColor = [UIColor colorWithHex:0x19cdc7].CGColor;
        [_reAnswerBtn setTitle:@"重新作答" forState:UIControlStateNormal];
        [_reAnswerBtn setTitleColor:[UIColor colorWithHex:0x19cdc7] forState:UIControlStateNormal];
        _reAnswerBtn.frame = CGRectMake((kWidth-144)/2,_tipHeight+_bottomY, 143,44); //CGRectMake((kWidth-140*ScalesW)/2,tipHeight+bottomY, 140*ScalesW, 35*ScalesW);
        [_reAnswerBtn addTarget:self action:@selector(reAnswerBtnAction:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    return _reAnswerBtn;
}


- (UIImageView *)answerWrongView {
    if (!_answerWrongView) {
//        _answerWrongView = [[UIImageView alloc] initWithFrame:CGRectMake((kWidth-110)/2,_topY,110,33)];
        _answerWrongView = [[UIImageView alloc] initWithFrame:CGRectMake((kWidth-110)/2,_bottomY+_tipHeight,110,33)];
        _answerWrongView.image = [UIImage imageNamed:@"LS-回答错误背景框"];
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.image = [UIImage imageNamed:@"LS-回答错误图标"];
        [_answerWrongView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_answerWrongView).offset(20);
            make.centerY.equalTo(_answerWrongView);
            make.size.mas_equalTo(CGSizeMake(20, 18));
        }];
        UILabel *label = [[UILabel alloc] init];
        label.text = @"回答错误";
        label.font =FONT(13);
        label.textColor = [UIColor colorWithHex:0xe15604];
        [_answerWrongView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(imageView.mas_right).offset(3);
            make.centerY.equalTo(_answerWrongView);
            make.size.mas_equalTo(CGSizeMake(60, 13));
        }];
    }
    return _answerWrongView;
}

- (UIView *)answerTrueView {
    if (!_answerTrueView) {
        //_answerTrueView = [[UIImageView alloc] initWithFrame:CGRectMake((kWidth-110)/2,_topY,110,33)];
        _answerTrueView = [[UIImageView alloc] initWithFrame:CGRectMake((kWidth-110)/2,_bottomY+_tipHeight,110,33)];
        _answerTrueView.image = [UIImage imageNamed:@"LS-回答正确背景框"];
        UIImageView *imageView = [[UIImageView alloc] init];
        imageView.image = [UIImage imageNamed:@"LS-回答正确图标"];
        [_answerTrueView addSubview:imageView];
        [imageView mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_answerTrueView).offset(20);
            make.centerY.equalTo(_answerTrueView);
            make.size.mas_equalTo(CGSizeMake(20, 18));
        }];
        UILabel *label = [[UILabel alloc] init];
        label.text = @"回答正确";
        label.font =FONT(13);
        label.textColor = [UIColor colorWithHex:0x02bed3];
        [_answerTrueView addSubview:label];
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(imageView.mas_right).offset(3);
            make.centerY.equalTo(_answerTrueView);
            make.size.mas_equalTo(CGSizeMake(60, 13));
        }];
    }
    return _answerTrueView;
}

/*倒计时*/
- (UIView *)tipsView {
    if (!_tipsView) {
        
        CGFloat y = kHeight-kNavgationBar-kBottomPadding-_tipHeight+2;
        _tipsView = [[UIView alloc] initWithFrame:CGRectMake(0,y, kWidth, _tipHeight)];
        _tipsView.backgroundColor = [UIColor whiteColor];
        
        if (self.answerState!=2) {
            [_tipsView addSubview:self.timeCircleView];
            [_tipsView addSubview:self.tipsLabel];
        }
    }
    return _tipsView;
}

- (UILabel *)tipsLabel {
    if (!_tipsLabel) {
        _tipsLabel = [[UILabel alloc] initWithFrame:CGRectMake(40,_tipHeight+_bottomY,kWidth-80,14)];
        _tipsLabel.textColor = [UIColor colorWithHex:0x4a4a4a];
        _tipsLabel.font = [UIFont systemFontOfSize:12.0f];
        _tipsLabel.textAlignment = NSTextAlignmentCenter;
        _tipsLabel.numberOfLines = 0;
    }
    return _tipsLabel;
}

- (WJTimeCircle *)timeCircleView {
    if (!_timeCircleView) {
        //_timeCircleView = [[WJTimeCircle alloc] initWithFrame:CGRectMake((kWidth-73)/2,_topY,73, 73)];
        _timeCircleView = [[WJTimeCircle alloc] initWithFrame:CGRectMake((kWidth-73)/2,_bottomY+_tipHeight,73, 73)];
        _timeCircleView.arcFinishColor = [UIColor colorWithHex:0xededed];
        _timeCircleView.arcUnfinishColor = [UIColor colorWithHex:0x50c6f9];
        _timeCircleView.arcBackColor = [UIColor clearColor];
        _timeCircleView.baseColor = [UIColor colorWithRed:250/255.0 green:250/255.0 blue:250/255.0 alpha:1];
        _timeCircleView.width = 3;
        _timeCircleView.isStartDisplay = YES;
        UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapTimeCircleEndAction:)];
        [_timeCircleView addGestureRecognizer:tap];
        _timeCircleView.userInteractionEnabled = YES;
    }
    return _timeCircleView;
}

/*标题栏*/
- (UIView *)titleBarView {
    if (!_titleBarView) {
        _titleBarView = [[UIView alloc] init];
        UILabel *label = [[UILabel alloc] init];
        [_titleBarView addSubview:label];
        label.font = FONT(13);
        label.textColor = [UIColor colorWithHex:0x333333];
        NSString *text = [NSString stringWithFormat:@"第%ld题(共%ld题)",self.currentIndex+1,self.allCount];
        
        if (self.answerState==2) {
            text = @"听力选择";
        }
        
        NSMutableAttributedString *attr = [[NSMutableAttributedString alloc] initWithString:text];
        NSRange range = [text rangeOfString:[NSString stringWithFormat:@"第%ld题",self.currentIndex+1]];
        
        if (self.answerState==2) {
            range = NSMakeRange(0, text.length);
        }
        
        [attr addAttribute:NSForegroundColorAttributeName value:[UIColor colorWithHex:0x0693c8] range:range];
        label.attributedText = attr;
        [label mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(_titleBarView).offset(11);
            make.centerY.equalTo(_titleBarView);
            make.size.mas_offset(CGSizeMake(100, 13));
        }];
        
        UILabel *line = [[UILabel alloc] init];
        line.backgroundColor = [UIColor colorWithHex:0xededed];
        [_titleBarView addSubview:line];
        [line mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(_titleBarView);
            make.bottom.equalTo(_titleBarView).offset(-0.5);
            make.height.mas_equalTo(0.5);
        }];
    }
    return _titleBarView;
}

/*作答区域*/
- (BKAnswerScorllView *)scrollView {
    if (!_scrollView) {
        CGFloat height = kHeight-kNavgationBar-kBottomPadding-35-_tipHeight;
        _scrollView = [[BKAnswerScorllView alloc] initWithFrame:CGRectMake(0,self.tableView.y+self.tableView.height,kWidth,height-self.tableView.height)];
        _scrollView.backgroundColor = [UIColor whiteColor];
        WeakObj(self);
        _scrollView.answerBlock = ^(NSDictionary *answerDict) {
            selfWeak.answerDict = [NSMutableDictionary dictionaryWithDictionary:answerDict];
            if (selfWeak.answerDict.count==self.model.TotalItemCount) {
                
                selfWeak.scrollView.userInteractionEnabled = NO;
                
                [UIView animateWithDuration:0.3 animations:^{

                    selfWeak.timeCircleView.y = selfWeak.bottomY+selfWeak.tipHeight;
                    selfWeak.tipsLabel.y = selfWeak.tipHeight+selfWeak.bottomY;
                    
                }completion:^(BOOL finished) {
                    
                    [selfWeak.timeCircleView removeFromSuperview];
                    selfWeak.timeCircleView = nil;
                    [selfWeak.tipsLabel removeFromSuperview];
                    selfWeak.tipsLabel = nil;
                    
                    [selfWeak showResult];
                }];
                
            }
        };
    }
    
    return _scrollView;
}

/*播放区域*/
- (UITableView *)tableView {
    if (!_tableView) {
         CGFloat tableViewHeight = kHeight-kNavgationBar-kBottomPadding-35-_tipHeight;
        _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0,35, kWidth,tableViewHeight) style:UITableViewStylePlain];
        _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _tableView.delegate = self;
        _tableView.dataSource = self;
//        _tableView.estimatedRowHeight = 50;
//        _tableView.rowHeight = UITableViewAutomaticDimension;
        [_tableView registerClass:[LSListenSelectCell class] forCellReuseIdentifier:@"LSListenSelectCell"];
    }
    
    return _tableView;
}

- (UIView*)createTableViewHeader {
    
    UIView *header = [[UIView alloc] initWithFrame:CGRectMake(0, 0,kWidth,156*ScalesW)];
    
    UIImageView *backimageView =[[UIImageView alloc] init];
    backimageView.userInteractionEnabled = YES;
    [header addSubview:backimageView];
    [backimageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(header).offset(20);
        make.centerX.equalTo(header);
        make.size.mas_equalTo(CGSizeMake(266*ScalesW,156*ScalesW));
    }];
    backimageView.image = [UIImage imageNamed:@"LS-音频播放鸽子背景"];
    
    LSCircleBtn *imagePlayerView= [[LSCircleBtn alloc] init];
    self.imagePlayerView = imagePlayerView;
    [imagePlayerView setBackgroundImage:[UIImage imageNamed:@"LS-音频"] forState:UIControlStateNormal];
    imagePlayerView.userInteractionEnabled = NO;
   // imagePlayerView.image = [UIImage imageNamed:@"音频"];
    [backimageView addSubview:imagePlayerView];
    [imagePlayerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(backimageView);
        make.left.equalTo(backimageView).offset(35);
        make.size.mas_equalTo(CGSizeMake(18, 17));
    }];
    if (self.answerState==2) {
        [imagePlayerView setBackgroundImage:[UIImage imageNamed:@"LS-播放"] forState:UIControlStateNormal];
        [imagePlayerView setBackgroundImage:[UIImage imageNamed:@"LS-暂停"] forState:UIControlStateSelected];
        imagePlayerView.userInteractionEnabled = YES;
        [imagePlayerView addTarget:self action:@selector(playOrPauseAudio:) forControlEvents:UIControlEventTouchUpInside];
    }
    
    UISlider *slider =[[UISlider alloc] init];
    self.slider = slider;
    slider.userInteractionEnabled = NO;
    UIImage *thumbImage = [UIImage imageNamed:@"LS-音频进度点"];
    [slider setThumbImage:thumbImage forState:UIControlStateHighlighted];
    [slider setThumbImage:thumbImage forState:UIControlStateNormal];
    
    [slider setMinimumTrackTintColor:[UIColor whiteColor]];
    [slider setMaximumTrackTintColor:[UIColor colorWithHex:0x1a8fd5]];
    if (self.answerState==2) {
        [slider addTarget:self action:@selector(sliderChanged) forControlEvents:UIControlEventValueChanged];
    }
  
    
    [backimageView addSubview:slider];
    [slider mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerY.equalTo(backimageView);
        make.left.equalTo(imagePlayerView.mas_right).offset(6);
        make.right.equalTo(backimageView).offset(-54);
    }];
    
    UILabel *timeLabel = [[UILabel alloc] init];
    self.timeLabel = timeLabel;
    timeLabel.text = [NSString stringWithFormat:@"00:00/%@",[self timeTransform:self.model.TimeLong]];
    [backimageView addSubview:timeLabel];
    timeLabel.textColor = [UIColor whiteColor];
    timeLabel.textAlignment = NSTextAlignmentLeft;
    timeLabel.font = [UIFont systemFontOfSize:8];
    [timeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(backimageView).offset(-39);
        make.top.equalTo(slider.mas_bottom).offset(0);
        make.width.mas_equalTo(53);
    }];
    
    if ((self.answerState==1&&self.isReAnswer==YES) || self.answerState==2) {
        LSTYAttributedLabel *quesBody = [[LSTYAttributedLabel alloc] init];
        [header addSubview:quesBody];
        
        LSAVdio *avdio = self.model.AudioList.firstObject;
        if (avdio.Article.length>0) {
            NSString *string = [NSString stringWithFormat:@"<font color=\"#525252\">听力原文:</font><P>%@",avdio.Article];
            if (self.model.ChildList.count==1) {
                string = [NSString stringWithFormat:@"<font color=\"#525252\">听力原文:</font><P>%@</P><BR><P><font size='4'>%@</font>",avdio.Article,self.model.ChildList.firstObject.ChildAsk];
            }
          
            quesBody.attributedText = [string dr_HtmlStringWithFontSize:14];
            quesBody.numberOfLines = 0;
            quesBody.linesSpacing = 5;
            quesBody.textColor = [UIColor blackColor];
            quesBody.lineBreakMode = NSLineBreakByWordWrapping;
            [quesBody sizeToFit];
            CGSize size = [quesBody getSizeWithWidth:kWidth-20];
            [quesBody mas_makeConstraints:^(MASConstraintMaker *make) {
                make.top.equalTo(header).offset(156*ScalesW-30);
                make.centerX.equalTo(header);
                make.width.mas_equalTo(kWidth-20);
                make.height.mas_equalTo(size.height+10);
            }];
            
            header.height = size.height + 30 + 156*ScalesW - 30;
            
            if (self.answerState==1) {
                quesBody.delegate = self;
                quesBody.isTextBackgroundColor = YES;
                quesBody.textBackgroundColor = [UIColor orangeColor];
                [self setupLabel:quesBody HightlightlyString:quesBody.attributedText.string Words:_mutlNameArr Codes:_mutlCodeArr];

//                LSGetZSDManager *manager = [[LSGetZSDManager alloc] init];
//                manager.requestParam = @{@"Text":avdio.Article,
//                                         @"stageNo":KGetUserDefaults(@"TestLayer")
//                                         };
//                WeakObj(self);
//                [MBProgressHUD showDefaultToView:quesBody];
//                [manager requestWithCompltionSuccess:^(NSArray *zsdArr) {
//
//                    NSMutableArray *mutlCodeArr = [NSMutableArray array];
//                    NSMutableArray *mutlNameArr = [NSMutableArray array];
//                    for (LSModelZSD *zsd in zsdArr) {
//                        [mutlCodeArr addObject:zsd.ZSDCode];
//                        [mutlNameArr addObject:zsd.ZSDName];
//                    }
//                    dispatch_async(dispatch_get_main_queue(), ^{
//                        [selfWeak setupLabel:quesBody HightlightlyString:quesBody.attributedText.string Words:mutlNameArr Codes:mutlCodeArr];
//                        [MBProgressHUD hideHUDForView:quesBody animated:YES];
//                    });
//
//                } failuer:^(NSString *error) {
//
//                }];
                
            }
        }
    } else {

        UILabel *quesBody = [[UILabel alloc] init];
        quesBody.numberOfLines = 0;
        quesBody.textColor = [UIColor blackColor];
        [header addSubview:quesBody];
        if (self.model.QuesBody.length==0) {
            self.model.QuesBody = @"";
        }
        NSString *text = @"";
        if (self.model.ChildList.count==1) {
            text = [NSString stringWithFormat:@"<P>%@<P><font size='4'>%@</font>", self.model.QuesBody,self.model.ChildList.firstObject.ChildAsk];
        } else {
            text = self.model.QuesBody;
        }
        quesBody.attributedText = [text dr_HtmlStringWithFontSize:14];
        CGSize size =[quesBody.attributedText boundingRectWithSize:CGSizeMake(kWidth-20, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin context:nil].size;
        [quesBody mas_makeConstraints:^(MASConstraintMaker *make) {
            make.top.equalTo(header).offset(156*ScalesW-30);
            make.centerX.equalTo(header);
            make.width.mas_equalTo(kWidth-20);
            make.height.mas_equalTo(size.height+10);
        }];
        
        header.height = size.height + 30 + 156*ScalesW - 30;
        
    }
    
    return header;
}

- (void)getZSDHightly:(NSString*)text {
    LSGetZSDManager *manager = [[LSGetZSDManager alloc] init];
    manager.requestParam = @{@"Text":text,
                             @"stageNo":KGetUserDefaults(@"TestLayer")
                             };
    WeakObj(self);
    [manager requestWithCompltionSuccess:^(NSArray *zsdArr) {
        
        selfWeak.mutlCodeArr = [NSMutableArray array];
        selfWeak.mutlNameArr = [NSMutableArray array];
        for (LSModelZSD *zsd in zsdArr) {
            [selfWeak.mutlCodeArr addObject:zsd.ZSDCode];
            [selfWeak.mutlNameArr addObject:zsd.ZSDNameInText];
        }
        
    } failuer:^(NSString *error) {
        
    }];
}

//**加载高亮**//
- (void)setupLabel:(LSTYAttributedLabel*)textLabel HightlightlyString:(NSString*)text Words:(NSArray*)words Codes:(NSArray*)codes{
    NSString *string = text;
    
    NSMutableArray *removeHadArr = [NSMutableArray array];
    [string enumerateSubstringsInRange:NSMakeRange(0, string.length-1) options:NSStringEnumerationByWords usingBlock:^(NSString * _Nullable substring, NSRange substringRange, NSRange enclosingRange, BOOL * _Nonnull stop) {
        
        if([words containsObject:substring] && ![removeHadArr containsObject:substring]) {
            
            [removeHadArr addObject:substring];
            
            NSInteger index = [words indexOfObject:substring];
            //NSString *linkData = codes[index];
            NSString *linkData = [NSString stringWithFormat:@"%@|%@",codes[index],substring];
            [textLabel addLinkWithLinkData:linkData linkColor:[UIColor whiteColor] underLineStyle:kCTUnderlineStyleNone range:substringRange];
            //textLabel.linesSpacing = 10;
        }
    }];
    
}

- (UIView*)createFooterView {
    BKListenAnswerView *footerView = [[BKListenAnswerView alloc] initWithFrame:CGRectMake(0, 0, kWidth, 128)];
    NSString *stuAnswer = [self.answerDict objectForKey:@"1"];
    NSString *trueAnswer = self.model.ChildList.firstObject.ChildAnswer;
    footerView.myAnswer = stuAnswer;
    footerView.tureAnswer = trueAnswer;
    
    return footerView;
}

#pragma mark - tableViewDelegate
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {

    LSOption *optionModel = self.model.ChildList.firstObject.ChildOptionList[indexPath.row];
    
    if (optionModel.cellHeight+17<=50) {
        return 50;
    }
    
    return optionModel.cellHeight+17;

}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    tableView.userInteractionEnabled = NO;
    LSChild *child = self.model.ChildList.firstObject;
    for (NSInteger i = 0; i<child.ChildOptionList.count; i++) {
        NSIndexPath *newIndexPath = [NSIndexPath indexPathForRow:i inSection:0];
        LSListenSelectCell *cell = [tableView cellForRowAtIndexPath:newIndexPath];
        if (i==indexPath.row) {
            cell.optionStyle = SelectOptionBlue;//选中
        } else {
            cell.optionStyle = SelectOptionWhite;
        }
    }
    
    NSString *answer = [NSString stringWithFormat:@"%c",(int)(indexPath.row+65)];
    if (self.answerDict==nil) {
        self.answerDict = [NSMutableDictionary dictionary];
    }
    [self.answerDict setObject:answer forKey:@"1"];
    
    [UIView animateWithDuration:0.3 animations:^{
        //self.timeCircleView.y = _topY;
        self.timeCircleView.y = _bottomY+_tipHeight;
        self.tipsLabel.y = _tipHeight+_bottomY;
    }completion:^(BOOL finished) {
        
        [self.timeCircleView removeFromSuperview];
        self.timeCircleView = nil;
        [self.tipsLabel removeFromSuperview];
        self.tipsLabel = nil;
        
        [self showResult];
    }];
    
//    //直接调用倒计时结束，分析评判结果
//    [self timeCircleEnd];
}


#pragma mark - tableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if(self.model.ChildList.count>1) {
        return 0;
    }
    
    return self.model.ChildList.firstObject.ChildOptionList.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    LSListenSelectCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LSListenSelectCell"];
    cell.optionStyle = SelectOptionWhite;
   // cell.textHeight  = ((NSNumber*)self.cellRowHeightArr[indexPath.row]).floatValue;
    cell.model = self.model.ChildList.firstObject.ChildOptionList[indexPath.row];
    
    if (self.answerState==1) {
        cell.userInteractionEnabled = NO;
        NSString *string = [NSString stringWithFormat:@"%c",(int)(indexPath.row+65)];
        NSString *stuAnswer = [self.answerDict objectForKey:@"1"];
        NSString *trueAnswer = self.model.ChildList.firstObject.ChildAnswer;
        if ([string isEqualToString:stuAnswer] && [stuAnswer isEqualToString:trueAnswer]) {//作答位置和正确位置一致
            cell.optionStyle = SelectOptionGreen;
        } else if ([string isEqualToString:stuAnswer] && ![stuAnswer isEqualToString:trueAnswer]) {
            cell.optionStyle = SelectOptionRed;
        }
    } else if (self.answerState==2) {
        cell.userInteractionEnabled = NO;
        NSString *string = [NSString stringWithFormat:@"%c",(int)(indexPath.row+65)];
        NSString *stuAnswer = [self.answerDict objectForKey:@"1"];
        NSString *trueAnswer = self.model.ChildList.firstObject.ChildAnswer;
        
        if ([string isEqualToString:stuAnswer] && ![stuAnswer isEqualToString:trueAnswer]) {
            cell.optionStyle = SelectOptionRed;
        } else if ([string isEqualToString:trueAnswer]) {
            cell.optionStyle = SelectOptionGreen;
        }
    } else {
        
        cell.userInteractionEnabled = YES;
        NSString *string = [NSString stringWithFormat:@"%c",(int)(indexPath.row+65)];
        NSString *stuAnswer = [self.answerDict objectForKey:@"1"];
        if ([string isEqualToString:stuAnswer]) {
            cell.optionStyle = SelectOptionBlue;
        }
    }
    
    return cell;
}

#pragma mark - BKAudioPlayerDelegate
- (void)audioPlayEnd {
    NSLog(@"////audioPlayEnd");
    _isPlaying = NO;
    self.slider.value = self.slider.maximumValue;
    if (self.answerState!=2) {
        self.step = 2;
        [self updateTipView];
        
    }   else if (self.answerState==2) {
        
        self.imagePlayerView.selected = NO;
        
    }

}

- (void)audioUpdateWith:(float)time Totaltime:(float)totalTime {
    _isPlaying = YES;
    
    CGFloat allTime = 0;
    if (self.model.TimeLong>totalTime) {
        allTime = totalTime;
    } else if (self.model.TimeLong<=totalTime) {
        allTime = self.model.TimeLong;
    }
    
    self.timeLabel.text = [NSString stringWithFormat:@"%@/%@",[self timeTransform:time],[self timeTransform:allTime]];
    self.slider.minimumValue = 0;
    self.slider.maximumValue = allTime;//self.model.TimeLong;
    self.slider.value = time;
    
//    if (time>=allTime && self.model.TimeLong<totalTime) {
//        self.player.player.rate = 0;
//        [self audioPlayEnd];
//        NSLog(@"self.model.TimeLong=%ld,totalTime=%f,xml中音频时长小于实际时长",self.model.TimeLong,totalTime);
//    }
    
}

- (NSString*)timeTransform:(CGFloat)time {
    NSString *minute = [NSString stringWithFormat:@"%02ld",(NSInteger)time/60];
    NSString *second = [NSString stringWithFormat:@"%02ld",(NSInteger)time%60];
    NSString *timeString = [NSString stringWithFormat:@"%@:%@",minute,second];
    return timeString;
}

#pragma mark - Private Method
- (void)sliderChanged {
    NSLog(@"slide=%f",self.slider.value);
    CGFloat recordTime = self.slider.value;
    [_player.player seekToTime:CMTimeMake(recordTime, 1)];
}

//开始倒计时
- (void)startTimeCircle {
    
    self.timeCircleView.totalSecond = self.second;
    
    self.tipsLabel.y = _bottomY + _tipHeight;
    [UIView animateWithDuration:0.3 animations:^{
        
        self.timeCircleView.y=5;
        self.tipsLabel.y = self.timeCircleView.y+5+self.timeCircleView.height;
        
    } completion:^(BOOL finished) {
        
    }];

    if (self.step==0) {
        self.tipsLabel.text = @"请浏览试题,倒计时结束后播放听力\n(点击可提前结束浏览时间)";
        self.tipsLabel.height = 30;
    }
    
    if (self.circleTimer) {
        [self.circleTimer invalidate];
        self.circleTimer = nil;
    }
    self.circleTimer = [NSTimer scheduledTimerWithTimeInterval:0.01 target:self selector:@selector(timeAnim) userInfo:nil repeats:YES];
    NSLog(@"////");
    [[NSRunLoop mainRunLoop] addTimer:_circleTimer forMode:NSRunLoopCommonModes];
   
}

- (void)timeAnim{
    self.second = self.second-0.01;
    //NSLog(@"===%f",self.second);
    if (self.second <= 0.00) {
        [self timeCircleEnd];
    } else {
        self.timeCircleView.second = self.second;
    }
}

//倒计时结束
- (void)timeCircleEnd {
    [self.circleTimer invalidate];
    self.circleTimer = nil;
    
    if (self.step==0) {
        //预读结束，准备播放音频
        self.step = 1;
        [self updateTipView];
        
    } else if (self.step==2) {
        //作答结束，准备分析作答结果
        [self.player closePlayer];
        
        [self showResult];
        
        [UIView animateWithDuration:0.3 animations:^{
            self.tipsLabel.y = _tipHeight + _bottomY;
        } completion:^(BOOL finished) {
            [self.tipsLabel removeFromSuperview];
            self.tipsLabel = nil;
        }];
    }
    
    [UIView animateWithDuration:0.3 animations:^{
       // self.timeCircleView.y = _topY;
        self.timeCircleView.y = _tipHeight + _bottomY;
//        self.tipsLabel.y = _tipHeight + _bottomY;
    } completion:^(BOOL finished) {
        
        [self.timeCircleView removeFromSuperview];
        self.timeCircleView = nil;
//        [self.tipsLabel removeFromSuperview];
//        self.tipsLabel = nil;
        
  #pragma mark - 涉及到时间动画，暂停会有问题，需要修改2
//        if (self.step==0) {
//            //预读结束，准备播放音频
//            self.step = 1;
//            [self updateTipView];
//
//        } else if (self.step==2) {
//            //作答结束，准备分析作答结果
//            [self.player closePlayer];
//
//            [self showResult];
//        }
    }];
    
}

- (void)tapTimeCircleEndAction:(UITapGestureRecognizer*)gesture {
    UIView *view = gesture.view;
    view.userInteractionEnabled = NO;
    [self timeCircleEnd];
}

- (void)updateTipView {
    //改变提示样式
    if (self.step == 1) {
        self.tipsLabel.text = @"听力播放中...";
        [self.tipsView addSubview:self.tipsLabel];
        [UIView animateWithDuration:0.3 animations:^{
            self.tipsLabel.y = 40;
        }];
        
         //播放音频
         //NSLog(@"%@",_model.AudioList.firstObject.AudioPath);
         NSURL *url = nil;
         if ([_from isEqualToString:@"A00"]) {
             url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",self.model.AudioList.firstObject.AudioPath]];
         } else {
             
             url = [NSURL fileURLWithPath:self.model.AudioList.firstObject.AudioPath];
         }
        
         [self.player playerUrl:url seekTotime:0 rateValue:_playRate];
    }
    else if (self.step == 2) {
        
        self.tipsLabel.text = @"请在倒计时结束前完成作答";
        //设置作答倒计时
        self.second = self.model.RecordTime+self.model.WaitTime;
        [self.tipsView addSubview:self.timeCircleView];
        [self.tipsView addSubview:self.tipsLabel];
        
        self.timeCircleView.userInteractionEnabled = NO;
        
        [self startTimeCircle];
    }
}

//展示结果
- (void)showResult {
    if (self.isPlaying) {
        [self.player playerPaused];
    }
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self animated:YES];
    hud.label.text = @"结果分析中...";
    double delayInSeconds = 0.5;
    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds *NSEC_PER_SEC);
    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
        //执行事件
        [self.circleTimer invalidate];
        self.circleTimer = nil;
        [self.timeCircleView removeFromSuperview];
        self.timeCircleView = nil;
        [self.player closePlayer];
        
        //刷新界面
        [self reloadUI];
        [MBProgressHUD hideHUDForView:self animated:YES];
    });
}

//刷新出正确或者错误界面
- (void)reloadUI {
    self.answerState = 1;
    
    if(self.model.ChildList.count>1) {
        self.scrollView.answerState = self.answerState;
        self.scrollView.answerDict = _answerDict;
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
        [self.scrollView reloadScorllView];
    }
    
    self.tableView.tableHeaderView = [self createTableViewHeader];
    self.tableView.userInteractionEnabled = YES;
    [self.tableView reloadData];
    
    //分析作答结果
    NSInteger count = 0;
    for (NSInteger i = 0; i<self.model.ChildList.count; i++) {
        LSChild *child = self.model.ChildList[i];
        NSString *stuAnswer = [self.answerDict objectForKey:[NSString stringWithFormat:@"%ld",i+1]];
        if ([stuAnswer isEqualToString:child.ChildAnswer]) {
            count ++;
        }
    }
    
    //返回代理
    CGFloat correctRate = (count*1.00)/(self.model.TotalItemCount*1.00);
    if (self.isReAnswer==NO) {
        if (self.delegate && [self.delegate respondsToSelector:@selector(FinishAnswerQues:CorrectRate:IsSave:)]) {
            [self.delegate FinishAnswerQues:self.answerDict CorrectRate:correctRate IsSave:NO];
        }
    }
    
    if (count==self.model.TotalItemCount) {//正确
        
        [self.tipsView addSubview:self.answerTrueView];
         if(self.isReAnswer) {//已经重新作答过了，显示下一题和重新作答
             
            [self.tipsView addSubview:self.nextBtn];
             
         } else {
             
            [self.tipsView addSubview:self.tipsLabel];
             self.second = 3;
             self.circleTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(enterNextQuesUpdateText) userInfo:nil repeats:YES];
             [[NSRunLoop mainRunLoop] addTimer:_circleTimer forMode:NSRunLoopCommonModes];
         }

        [UIView animateWithDuration:0.3 animations:^{
            self.answerTrueView.y = 20;
            if (self.isReAnswer==NO) {// //没有过重新作答，直接进入下一题

                self.tipsLabel.y = 60;
                self.tipsLabel.text = [NSString stringWithFormat:@"%d秒后进入下一题",(int)self.second];
                if (self.currentIndex+1 == self.allCount) {
                    self.tipsLabel.text = [NSString stringWithFormat:@"%d秒后进入查看结果",(int)self.second];
                    //代理回调
                    if(self.moudleID==2) {
                        
                        [self.circleTimer invalidate];
                        self.circleTimer = nil;
                        self.tipsLabel.text = @"";
                        
                        if(self.delegate && [self.delegate respondsToSelector:@selector(retrunShowComplteView)]) {
                            [self.delegate retrunShowComplteView];
                        }
                    }
                }

            } else { //第二遍作对了
                
                self.nextBtn.y = _tipHeight-self.nextBtn.height-15;
                
                 if (self.currentIndex+1 == self.allCount) {
                     //代理回调
                     if(self.moudleID==2) {
                         
                         [self.circleTimer invalidate];
                         self.circleTimer = nil;
                         self.tipsLabel.text = @"";
                         
                         if(self.delegate && [self.delegate respondsToSelector:@selector(retrunShowComplteView)]) {
                             [self.delegate retrunShowComplteView];
                         }
                     }
                 }
              
            }
        
        } completion:^(BOOL finished) {
        }];
        
        
    } else { //错误
        

        [self.tipsView addSubview:self.answerWrongView];
        if(self.isReAnswer) {//已经重新作答过了，显示下一题和重新作答
           
            [self.tipsView addSubview:self.nextBtn];
            self.nextBtn.x = self.nextBtn.x + self.nextBtn.width/2 + 10;
        
            [self.tipsView addSubview:self.reAnswerBtn];
            self.reAnswerBtn.x = self.reAnswerBtn.x - self.reAnswerBtn.width/2 - 10;
            
        } else {
            
            [self.tipsView addSubview:self.tipsLabel];
            self.second = 3;
            self.circleTimer = [NSTimer scheduledTimerWithTimeInterval:1 target:self selector:@selector(updateTipLabeltext) userInfo:nil repeats:YES];
            [[NSRunLoop mainRunLoop] addTimer:_circleTimer forMode:NSRunLoopCommonModes];
        }

        [UIView animateWithDuration:0.3 animations:^{
            self.answerWrongView.y = 20;
            if(self.isReAnswer==NO) {
                //没有过重新作答，直接进入重新作答
                self.tipsLabel.y = 60;
                self.tipsLabel.text = [NSString stringWithFormat:@"%d秒后重新作答",(int)self.second];
            } else {
                
                self.nextBtn.y = _tipHeight-self.nextBtn.height-15;
                self.reAnswerBtn.y = _tipHeight-self.reAnswerBtn.height-15;
                
                if (!self.isUseReAnswerBtn && (self.currentIndex+1 == self.allCount)) {
                    if(self.delegate && [self.delegate respondsToSelector:@selector(retrunShowComplteView)]) {
                        [self.delegate retrunShowComplteView];
                    }
                }
            }

        } completion:^(BOOL finished) {
        }];
        
    }
    //返回代理
//    CGFloat correctRate = (count*1.00)/(self.model.TotalItemCount*1.00);
//    if (self.isReAnswer==NO) {
//        if (self.delegate && [self.delegate respondsToSelector:@selector(FinishAnswerQues:CorrectRate:IsSave:)]) {
//            [self.delegate FinishAnswerQues:self.answerDict CorrectRate:correctRate IsSave:NO];
//        }
//    }
}

//重新作答倒计时
- (void)updateTipLabeltext {
    
    self.second--;
    self.tipsLabel.text = [NSString stringWithFormat:@"%d秒后重新作答",(int)self.second];
    NSLog(@"%f",self.second);
    if ((int)self.second<=-1) {

        [self.circleTimer invalidate];
        self.circleTimer = nil;
        
        self.second = self.model.ReadTime;
        [self.tipsView addSubview:self.timeCircleView];
        self.step = 0;
        [self startTimeCircle];
        [self reAnswerUI];
        
        [UIView animateWithDuration:0.3 animations:^{
            
           // self.answerWrongView.y = _topY;
            self.answerWrongView.y = _tipHeight + _bottomY;
 //           self.tipsLabel.y = tipHeight + bottomY;
            
        } completion:^(BOOL finished) {
        
//            [self.tipsLabel removeFromSuperview];
//            self.tipsLabel = nil;
            
            [self.answerWrongView removeFromSuperview];
            self.answerWrongView = nil;
        }];
    }
}

//进入下一题倒计时
- (void)enterNextQuesUpdateText {
    self.second--;
    self.tipsLabel.text = [NSString stringWithFormat:@"%d秒后进入下一题",(int)self.second];
    if (self.currentIndex+1 == self.allCount) {
        self.tipsLabel.text = [NSString stringWithFormat:@"%d秒后进入查看结果",(int)self.second];
    }
    
    if ((int)self.second<=0) {
        
        [self.circleTimer invalidate];
        self.circleTimer = nil;
        
        [UIView animateWithDuration:0.3 animations:^{

           // self.answerTrueView.y = _topY;
            self.answerTrueView.y = _tipHeight + _bottomY;
            self.tipsLabel.y = _tipHeight + _bottomY;
            
        }completion:^(BOOL finished) {
            
            [self.tipsLabel removeFromSuperview];
            self.tipsLabel = nil;
            
            [self.answerTrueView removeFromSuperview];
            self.answerTrueView = nil;

            if(self.delegate && [self.delegate respondsToSelector:@selector(willEnterNextQues)]) {
                [self.delegate willEnterNextQues];
            }
            
        }];
        
    }
}

//重新作答
- (void)reAnswerUI {
    
    if (self.mutlCodeArr.count==0) {
        [self getZSDHightly:_model.AudioList.firstObject.Article];
    }
    self.isReAnswer = YES;
    [self.answerWrongView removeFromSuperview];

    //重置结果，阶段
    [_answerDict removeAllObjects];
    self.answerState = 0;
    _step = 0;
    
    //界面刷新
    if(self.model.ChildList.count>1) {
        self.scrollView.answerState = self.answerState;
        self.scrollView.answerDict = _answerDict;
        [self.scrollView reloadScorllView];
        [self.scrollView setContentOffset:CGPointMake(0, 0) animated:YES];
    }
    
    self.tableView.tableHeaderView = [self createTableViewHeader];
    [self.tableView reloadData];

    //重置倒计时
//    self.second = _model.ReadTime;
//    [self.tipsView addSubview:self.timeCircleView];
//    [self.tipsView addSubview:self.tipsLabel];
//
//    [self startTimeCircle];
    
    
    //延迟1s倒计时
//    double delayInSeconds = 1.0;
//    dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, delayInSeconds *NSEC_PER_SEC);
//    dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
//        //执行事件
//        [self startTimeCircle];
//    });
}

#pragma mark - ButtonAction
- (void)reAnswerBtnAction:(id)sender {
    
    self.isUseReAnswerBtn = YES;
    
    self.step = 0;
    self.second = self.model.ReadTime;
    [self.tipsView addSubview:self.timeCircleView];
    [self.tipsView addSubview:self.tipsLabel];
    [self startTimeCircle];
    [self reAnswerUI];
    
    [UIView animateWithDuration:0.3 animations:^{
        
       // self.answerWrongView.y = _topY;
        self.answerWrongView.y = _tipHeight + _bottomY;
        self.reAnswerBtn.y = _tipHeight + _bottomY;
        self.nextBtn.y = _tipHeight + _bottomY;
        
    } completion:^(BOOL finished) {
        [self.answerWrongView removeFromSuperview];
        self.answerWrongView = nil;
        
        [self.reAnswerBtn removeFromSuperview];
        self.reAnswerBtn = nil;
        
        [self.nextBtn removeFromSuperview];
        self.nextBtn = nil;
        
    }];
}

- (void)nextBtnAction:(id)sender {
    
    [UIView animateWithDuration:0.3 animations:^{
//        self.answerWrongView.y = _topY;
//        self.answerTrueView.y = _topY;
        self.answerWrongView.y = _tipHeight + _bottomY;
        self.answerTrueView.y = _tipHeight + _bottomY;
        self.reAnswerBtn.y = _tipHeight + _bottomY;
        self.nextBtn.y = _tipHeight + _bottomY;
    } completion:^(BOOL finished) {
        [self.answerWrongView removeFromSuperview];
        self.answerWrongView = nil;
        
        [self.answerTrueView removeFromSuperview];
        self.answerTrueView = nil;
        
        [self.reAnswerBtn removeFromSuperview];
        self.reAnswerBtn = nil;
        
        [self.nextBtn removeFromSuperview];
        self.nextBtn = nil;
        
        if (self.delegate && [self.delegate respondsToSelector:@selector(willEnterNextQues)]) {
            [self.delegate willEnterNextQues];
        }
    }];
}

- (void)playOrPauseAudio:(UIButton*)sender {
    sender.selected = !sender.selected;
    if (sender.selected) {
        [self.player playerPlay];
        if (_isFirstPlay || self.slider.value==self.slider.maximumValue) {
            self.slider.userInteractionEnabled = YES;
            NSURL *url = nil;
            if ([_from isEqualToString:@"A00"]) {
                url = [NSURL URLWithString:[NSString stringWithFormat:@"%@",self.model.AudioList.firstObject.AudioPath]];
            } else {
                
                url = [NSURL fileURLWithPath:self.model.AudioList.firstObject.AudioPath];
            }
            
            if (_playRate == 0) {
                _playRate = 1;
            }
            [self.player playerUrl:url seekTotime:0 rateValue:_playRate];
            _isFirstPlay = NO;
        }
        
    } else {
        [self.player playerPaused];
    }
}

#pragma mark - 取消定时器/暂停
//清除时间和播放器
- (void)TimeReleaseAction{
    NSLog(@"TimeReleaseAction");
    [self.circleTimer invalidate];
    self.circleTimer = nil;
    [self.player closePlayer];
}

- (void)TimePauseAction {
    [self.circleTimer setFireDate:[NSDate distantFuture]];
    if (self.isPlaying) {
        [self.player playerPaused];
    }
}

- (void)TimeResumeAction {
    [self.circleTimer setFireDate:[NSDate distantPast]];
     if (self.isPlaying) {
         [self.player playerPlay];
     }
}


#pragma mark - LSTYAttributedLabelDelegate
- (void)attributedLabel:(LSTYAttributedLabel *)attributedLabel textStorageClicked:(id<LSTYTextStorageProtocol>)textStorage atPoint:(CGPoint)point atRect:(CGRect)rect {
    if ([textStorage isKindOfClass:[LSTYLinkTextStorage class]]) {
        NSString *linkStr = ((TYLinkTextStorage*)textStorage).linkData;
        NSLog(@"%@",linkStr);
        
        _dictView = [[DicMainView alloc] initWithTestType:DictTestType_Word isShowRecord:YES];
        _dictView.delegate = self;
        _dictView.dictUrl = [NSString stringWithFormat:@"%@api/ZXYYTS/W_ZXYYTS_GetCoursewareByCode",KGetUserDefaults(@"SystemIP")];
        _dictView.dictParam = @{
                                @"knowledge":[linkStr componentsSeparatedByString:@"|"].firstObject,
                                @"levelCode":KGetUserDefaults(@"TestLayer"),
                                @"stageNo":KGetUserDefaults(@"TestLayer"),
                                @"schoolId":DR_UserInfo.SchoolID,
                                @"stuId":DR_UserInfo.UserID,
                                };

        [_dictView popAnimated:YES];
        
        LSSaveZSDManager *manager = [[LSSaveZSDManager alloc] init];
        
        NSDictionary *dict = @{@"StageNo":KGetUserDefaults(@"TestLayer"),
                               @"SchoolID":KGetUserDefaults(@"SchoolID"),
                               @"StuID":DR_UserInfo.UserID,
                               @"VocabularyID":[linkStr componentsSeparatedByString:@"|"].firstObject,
                               @"Vocabulary":[linkStr componentsSeparatedByString:@"|"].lastObject,
                               @"IsPassed":@0,
                               @"ReviewPassedCount":@0,
                               @"NeedReviewCount":@3,
                               @"CreateTime":[NSDate date] ,
                               @"UpdateTime":[NSDate date]
                               };
        manager.requestParam = @{
                                 @"stageNo":KGetUserDefaults(@"TestLayer"),
                                 @"schoolId":DR_UserInfo.SchoolID,
                                 @"stuId":DR_UserInfo.UserID,
                                 @"ModelUnfamiliarVocabularyList":@[dict]
                                 };
        
        [manager requestSaveWithCompltionSuccess:^(NSString *string) {
            NSLog(@"保存知识点");
        } failuer:^(NSString *error) {
            
        }];
        
    }

}

#pragma mark - DicMainViewDelegate
- (void)colseDicMainView:(UIView *)dictView {
    [dictView removeFromSuperview];
    dictView = nil;
}

- (void)stopClickNextBtn {
    [self.nextBtn.button setTitle:@"没有更多" forState:UIControlStateNormal] ;
    self.nextBtn.button.backgroundColor = [UIColor colorWithHex:0xe1e2e2];
    self.nextBtn.enabled = NO;
    _isNoMore = YES;
}

@end
